package net.oschina.app;

import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;

import com.robotium.solo.Solo;

import net.oschina.app.com.oschina.pagehelper;

/**
 * Created by Administrator on 2017/1/5.
 */

public class BasicTest extends ActivityInstrumentationTestCase2{

    private Boolean isNeedLogin = false;
    private Solo solo1;
    private SoloWrap solo;
    protected pagehelper pagehelper;

    public BasicTest(Boolean isNeedLogin){
        super(AppStart.class);
        this.isNeedLogin = isNeedLogin;
    }

    public BasicTest(){
        super(AppStart.class);
    }

    @Override
    public void setUp() throws Exception {
        try{
            super.setUp();
            solo1 = new Solo(getInstrumentation(),getActivity());
            solo = new SoloWrap(solo1);
            pagehelper = new pagehelper(solo);
            pagehelper.getNavigationPage().clickMe();
            if(isNeedLogin){
                if(!pagehelper.loginPage().isLogined("wuyy1992")){
                    pagehelper.loginPage().clickIcon();
                    pagehelper.loginDetailPage().login("2277144876@qq.com","yan1992");
                }
            }else {
                if(pagehelper.loginPage().isLogined("wuyy1992")){
                    pagehelper.loginPage().clickSettings();
                    pagehelper.settingPage().clickLayout();
                }
            }
            pagehelper.getNavigationPage().clickNews();
        }catch (Exception ex){
            Log.i("oschina", "错误发生在setup阶段,错误信息是"+ex.getMessage());
            solo.takeScreenshot(this.getClass().getSimpleName());
            tearDown();
            throw ex;
        }

    }

    @Override
    public void tearDown() throws Exception {
        try {

        }catch (Exception ex){
            Log.i("oschina", "错误发生在tearDown阶段,错误信息是"+ex.getMessage());
            solo.takeScreenshot(this.getClass().getSimpleName());
            tearDown();
        }finally {
            solo1.finishOpenedActivities();
            super.tearDown();
        }

    }
}
